<?php


namespace kernel;

/** 命令的参数数据类 */
class ArgvData
{
    /** @var array 基础数据 */
    public $data = [];

    /** @var string 调用的文件 */
    public $file = '';

    /** @var string 调用的方法 */
    public $action = '';

    /** @var string 调用方法传过来的参数 */
    public $param = [];

    public function __construct()
    {
        global $argv;
        $this->init($argv);
    }

    protected function init(array $argv)
    {
        $param = $argv;
        $this->data = $argv;
        $this->file = $argv[0];
        $this->action = $argv[1]??'';
        unset($param[0], $param[1]);
        $this->param = array_values($param);
    }

    public function getData()
    {
        return [
            'file' => $this->file,
            'action' => $this->action,
            'param' => $this->param,
        ];
    }

    public function __toString()
    {
        return json_encode($this->getData(), JSON_UNESCAPED_UNICODE);
    }

}